package com.ss.android.article.browser.download;

import android.content.Context;
import android.os.Environment;
import android.text.TextUtils;
import android.util.Log;
import android.webkit.URLUtil;

import com.a.a.s;
import com.ss.android.article.browser.download.internal.DownloadOperator;
import com.ss.android.article.browser.download.internal.DownloadState;
import com.ss.android.article.browser.download.internal.DownloadDBHelper;
import com.ss.android.article.browser.download.internal.DownloadListener;
import com.ss.android.article.browser.download.internal.DownloadTask;
import com.ss.android.common.app.AbsApplication;

import java.io.File;
import java.util.HashMap;
import java.util.Iterator;
import java.util.concurrent.CopyOnWriteArraySet;

public final class DownloadTaskManager {
    public static final String a = Environment.getExternalStorageDirectory().getPath() + "/download/";
    private static DownloadTaskManager instance;

    public DownloadDBHelper mDownloadDBHelper;
    public HashMap<DownloadTask, DownloadOperator> mDownloadOperatorMap;
    public HashMap<DownloadTask, CopyOnWriteArraySet<DownloadListener>> mDownloadListenerMap;
    public DownloadListener e;

    private DownloadTaskManager(Context context) {
        this.mDownloadOperatorMap = new HashMap();
        this.mDownloadListenerMap = new HashMap();
        this.mDownloadDBHelper = new DownloadDBHelper(context, "download.db");
        s v0 = this.b();
        v0.b = android.support.design.a.c();
        v0.c = android.support.design.a.c();
        v0.a(new j(this));
    }

    public static DownloadTaskManager getInstance() {
        synchronized (DownloadTaskManager.class) {
            if (DownloadTaskManager.instance == null) {
                DownloadTaskManager.instance = new DownloadTaskManager(AbsApplication.getApplication());
            }
        }
        return instance;
    }

    static DownloadDBHelper a(DownloadTaskManager arg1) {
        return arg1.mDownloadDBHelper;
    }

    private static String a(String arg6, String arg7, String arg8) {
        String v1 = null;
        if (TextUtils.isEmpty(((CharSequence) arg8))) {
            arg8 = URLUtil.guessFileName(arg6, v1, v1);
        }

        File v3 = new File(arg7 + File.separator + arg8);
        String v0 = "";
        if (arg8.contains(".")) {
            v1 = arg8.substring(0, arg8.indexOf(".") - 1);
            v0 = arg8.substring(arg8.indexOf(".") + 1, arg8.length());
            arg8 = v1;
        }

        int v1_1 = 0;
        File v2;
        StringBuilder v4;
        String v3_1;
        for (v2 = v3; v2.exists(); v2 = new File(v4.append(v3_1).toString())) {
            ++v1_1;
            v4 = new StringBuilder().append(arg7).append(File.separator).append(arg8).append("(").append(v1_1).append(")");
            v3_1 = TextUtils.isEmpty(v0) ? "" : "." + v0;
        }

        return v2.getName();
    }

    public static boolean a(String arg2) {
        File v0 = new File(arg2);
        boolean v0_1 = v0.exists() ? v0.delete() : false;
        return v0_1;
    }

    static HashMap b(DownloadTaskManager arg1) {
        return arg1.mDownloadOperatorMap;
    }

    public final void a(DownloadTask arg4, boolean arg5) {
        if (arg4.fileDir == null || arg4.fileDir.trim().length() == 0) {
            Log.w("DownloadTaskManager", "file path is invalid. file path : " + arg4.fileDir + ", use default file path : " + DownloadTaskManager.a);
            arg4.fileDir = DownloadTaskManager.a;
        }

        if (arg4.fileName != null && arg4.fileName.trim().length() != 0) {
            if (arg4.url != null && (URLUtil.isNetworkUrl(arg4.url))) {
                if (this.mDownloadOperatorMap.containsKey(arg4)) {
                    Log.w("DownloadTaskManager", "task existed");
                } else {
                    if (this.mDownloadListenerMap.get(arg4) == null) {
                        this.mDownloadListenerMap.put(arg4, new CopyOnWriteArraySet());
                    }

                    arg4.downloadState = DownloadState.INITIALIZE;
                    String v0 = DownloadTaskManager.a(arg4.url, arg4.fileDir, arg4.fileName);
                    arg4.fileName = v0;
                    arg4.title = v0;
                    Log.d("DOWNLOAD", arg4.fileName);
                    this.insertDatabase(arg4);
                    DownloadOperator v1 = arg5 ? new DownloadOperator(this, arg4) : new DownloadOperator(this, arg4, (byte) 0);
                    this.mDownloadOperatorMap.put(arg4, v1);
                    Iterator v2 = this.e(arg4).iterator();
                    while (v2.hasNext()) {
                        ((DownloadListener) v2.next()).f(arg4);
                    }

                    DownloadListener v0_1 = this.e;
                    if (v0_1 != null) {
                        v0_1.f(arg4);
                    }

                    v1.startDownload();
                }

                return;
            }

            Log.w("DownloadTaskManager", "invalid http url: " + arg4.url);
            throw new IllegalArgumentException("invalid http url");
        }

        Log.w("DownloadTaskManager", "file name is invalid. file name : " + arg4.fileName);
        throw new IllegalArgumentException("file name is invalid");
    }

    public final void a(DownloadTask arg5) {
        if (this.mDownloadOperatorMap.containsKey(arg5)) {
            Object v0 = this.mDownloadOperatorMap.get(arg5);
            Log.i("DownloadOperator", "pause download.");
            ((DownloadOperator) v0).c = true;
            ((DownloadOperator) v0).d = false;
            ((DownloadOperator) v0).mDownloadTask.downloadState = DownloadState.PAUSE;
            if (!((DownloadOperator) v0).e) {
                Iterator v2 = ((DownloadOperator) v0).mDownloadTaskManager.e(((DownloadOperator) v0).mDownloadTask).iterator();
                while (v2.hasNext()) {
                    ((DownloadListener) v2.next()).c(((DownloadOperator) v0).mDownloadTask);
                }

                DownloadListener v1 = ((DownloadOperator) v0).mDownloadTaskManager.e;
                if (v1 != null) {
                    v1.c(((DownloadOperator) v0).mDownloadTask);
                }
            }
        }
    }

    public final s b() {
        return s.a(new k(this));
    }

    public final void insertDatabase(DownloadTask downloadTask) {
        this.mDownloadDBHelper.getWritableDatabase().insert("download", null, DownloadDBHelper.toContentValues(downloadTask));
    }

    public final void updateDatabase(DownloadTask downloadTask) {
        this.mDownloadDBHelper.getWritableDatabase().update("download",
                DownloadDBHelper.toContentValues(downloadTask), "file_path=?", new String[]{downloadTask.getFilePath()});
    }

    public final boolean c() {
        Iterator v1 = this.mDownloadOperatorMap.keySet().iterator();
        do {
            if (v1.hasNext()) {
                Object v0 = v1.next();
                if (v0 == null) {
                    continue;
                }

                if (((DownloadTask) v0).downloadState == DownloadState.PAUSE) {
                    continue;
                }

                if (((DownloadTask) v0).downloadState == DownloadState.FAILED) {
                    continue;
                }

                break;
            } else {
                return true;
            }
        }
        while (true);

        boolean v0_1 = false;
        return v0_1;
    }

    public final void d(DownloadTask arg7) {
        if (arg7.downloadState != DownloadState.FINISHED) {
            Iterator v1 = this.e(arg7).iterator();
            while (v1.hasNext()) {
                ((DownloadListener)v1.next()).d(arg7);
            }

            DownloadListener v0 = this.e;
            if (v0 != null) {
                v0.d(arg7);
            }

            this.e(arg7).clear();
        }

        this.mDownloadOperatorMap.remove(arg7);
        this.mDownloadListenerMap.remove(arg7);
        this.mDownloadDBHelper.getWritableDatabase().delete("download", "file_path=?", new String[]{arg7.getFilePath()});
    }

    public final CopyOnWriteArraySet<DownloadListener> e(DownloadTask arg2) {
        Object v0;
        if (this.mDownloadListenerMap.get(arg2) != null) {
            v0 = this.mDownloadListenerMap.get(arg2);
        } else {
            v0 = new CopyOnWriteArraySet<>();
        }

        return ((CopyOnWriteArraySet) v0);
    }
}

